package com.wbdgj.ui.booking;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.views.RichTextView;

import java.text.DecimalFormat;

import butterknife.BindView;
import retrofit2.Call;

public class BookingInfo02Fragment extends BaseFragment {
    private Context context;

    @BindView(R.id.branch_profile)
    RichTextView branch_profile;
    @BindView(R.id.phone)
    TextView phone;

    @Override
    protected int getLayout() {
        return R.layout.ui_booking_info02;
    }

    @Override
    protected void onInit() {
        context = getActivity();
        hotel_info();

        findViewById(R.id.call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (ContextCompat.checkSelfPermission(context,
                            Manifest.permission.CALL_PHONE)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, 10);
                    }else{
                        Intent intent =new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone.getText().toString()));
                        startActivity(intent);
                    }
                }else{
                    Intent intent =new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone.getText().toString()));
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void fetchData() {

    }

    private void hotel_info() {
        Call<ResultObjectModel> call01 = HttpAdapter.getSerives().hotel_info(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID));
        call01.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    branch_profile.setRichText(linkedTreeMap.get("branch_profile")+"");
                    phone.setText(linkedTreeMap.get("branch_phone")+"");
                }
            }
        });
    }
}
